package com.yyj1205.jsongenrator.service.Implement;

import com.yyj1205.jsongenrator.config.ErineBotConfig;
import com.yyj1205.jsongenrator.service.FileService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

/**
 * @Title: FileServiceImpl
 * @Author yyj1205
 * @Package com.yyj1205.jsongenrator.service.Implement
 * @Date 2025-01-19 16:19
 * @description:
 */
@Service
public class FileServiceImpl implements FileService {
    @Autowired
    ErineBotConfig erineBotConfig;

    @Override
    public boolean saveFile(String content, String path) {
        try {
            FileUtils.writeStringToFile(new File(path),content,"UTF-8",false);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    @Override
    public boolean saveModels(String name, String userName,String content) {
        return saveFile(content,erineBotConfig.getBase_path() + File.separator + userName + File.separator + "model" + File.separator + name + File.separator + name + ".mod.json");
    }

    @Override
    public boolean saveTables(String name, String userName,String content) {
        return saveFile(content,erineBotConfig.getBase_path() + File.separator + userName + File.separator + "table" + File.separator + name + File.separator + name + ".tab.json");
    }

    @Override
    public boolean saveForms(String name,String userName,String content) {
        return saveFile(content,erineBotConfig.getBase_path() + File.separator + userName + File.separator + "form" + File.separator + name + File.separator + name + ".form.json");
    }
}
